<?php

class bobAutoCompGenerator {

	public $primary;
	public $callback = array();

	public $data = array(
		'extension' => array(),
		'function' => array(),
		'class' => array(),
		'method' => array()
	);
	
	public $count = array(
		'extension' => 0,
		'function' => 0,
		'class' => 0,
		'method' => 0	
	);
	
	public function __construct() {

		$this->data['extension'] = get_loaded_extensions();
		$this->count['extension'] = count($this->data['extension']);

		foreach(get_declared_classes() as $class) {
			$this->data['class'][$class] = true;
		}

		foreach($this->data['extension'] as $extension) {
			$ref = new reflectionExtension($extension);
			foreach($ref->getFunctions() as $function) {
				$this->data['function'][$function->name] = array(
					'extension' => $extension				
				);
				++$this->count['function'];
			}

			foreach($ref->getClasses() as $class) {
				if(array_key_exists($class->name,$this->data['class'])) { continue; }
				$this->data['class'][$class->name] = array(
					'extension' => $extension				
				);
				++$this->count['class'];
				
				foreach($class->getMethods() as $method) {
					$this->data['method'][$method->name] = array(
						'class' => $class->name					
					);
					++$this->count['method'];
				}
			}
						
			$ref = null;
		}
		
		$this->primary = array_merge($this->data['function'],$this->data['class']);
		$this->secondary &= $this->data['method'];
		
		bob::debugfln("[autocomp] {$this->count['extension']} PHP extensions loaded");
		bob::debugfln("[autocomp] {$this->count['function']} Functions indexed");
		bob::debugfln("[autocomp] {$this->count['class']} Classes indexed");
		bob::debugfln("[autocomp] {$this->count['method']} Methods indexed");

		return;
	}
	
	public function search($type,$word,$case = false) {
		if($type == 'secondary') { return; }

		//. quick disable for now.
		return;
		
		bob::debugfln("searching for {$type} {$word}");
		
		if(!$case)
			$word = strtolower($word);
		
		$exact = false;
		$match = array();
		$wlen = strlen($word);
		foreach($this->{$type} as $key => &$value) {
			if(strtolower(substr($key,0,$wlen)) == $word) {
				if(!$case) {
					$match[$lower = strtolower($key)] = $key;
					if($lower == $word) { $exact = true; }
				} else {
					$match[$key] = $key;
					if($key == $word) { $exact = true; }
				}
			}
		}
		
		if($exact) {
			if(!$case) {
				$match = array(($lower = strtolower($word)) => $word) + $match;
				unset($match[$lower]);
			}
			
			else {
			
			}
		}
		
		return $match;
	}
}

?>